package com.globalresources.ui.struts.action.message;

import java.util.List;

import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.userdetails.UserDetails;

import com.globalresources.business.inface.IMessageService;
import com.globalresources.business.inface.IUserService;
import com.globalresources.commons.AbstractAction;
import com.globalresources.commons.Constant;
import com.globalresources.commons.Pager;
import com.globalresources.commons.PagerService;
import com.globalresources.commons.vo.FileWrapper;
import com.globalresources.po.Friend;
import com.globalresources.po.User;
import com.lowagie.tools.concat_pdf;

/**
 * @author aaron.lu(luzhiyuan) E-mail:atyuan.lu@gmail.com
 * @since 创建时间：Aug 6, 2009 4:21:30 PM
 */
public class MessageAction extends AbstractAction {
	private IMessageService messageService;
	private IUserService userService;
	private PagerService pagerService;
	
	protected List<Friend> friends;
	protected Pager friends_pager;
	protected String friends_pagerMethod;
	protected String friends_totalRow;
	protected String friends_currentPage;
	protected Friend friend_;
	
	protected List<Friend> backlist;
	protected Pager backlist_pager;
	protected String backlist_pagerMethod;
	protected String backlist_totalRow;
	protected String backlist_currentPage;
	protected Friend back_friend;
	
	protected String delete_friend_id;
	protected String update_friend_id;
	
	/**
	 * 获取好友列表
	*@author aaron.lu(luzhiyuan)
	*@return
	 */
	public String list(){
		String current_user = getCurrentUerName();
		Long count = messageService.countFriends(current_user, Constant.BOOLEAN_FALSE);
		friends_pager = pagerService.getPager(friends_currentPage, friends_pagerMethod, count.intValue());
		setFriends_currentPage(String.valueOf(friends_pager.getCurrentPage()));
		setFriends_totalRow(count.toString());
		friends = messageService.getFriends(null, current_user, Constant.BOOLEAN_FALSE, friends_pager.getPageSize(), friends_pager.getStartRow());
		return SUCCESS;
	}
	
	/**
	 * 获取黑名单列表
	*@author aaron.lu(luzhiyuan)
	*@return
	 */
	public String backlist(){
		Long count = messageService.countFriends(getCurrentUerName(), Constant.BOOLEAN_TRUE);
		backlist_pager =pagerService.getPager(backlist_currentPage, backlist_pagerMethod, count.intValue());
		setBacklist_currentPage(String.valueOf(backlist_pager.getCurrentPage()));
		setBacklist_totalRow(count.toString());
		backlist = messageService.getFriends(null, getCurrentUerName(), Constant.BOOLEAN_TRUE, friends_pager.getPageSize(), friends_pager.getStartRow());
		return SUCCESS;
	}
	
	/**
	 * 加载好友详细信息
	*@author aaron.lu(luzhiyuan)
	*@return
	 */
	public String load_friend(){
		
		return SUCCESS;
	}
	
	/**
	 * <P>保存用户增的好友信息
	*@author aaron.lu(luzhiyuan)
	*@return
	 */
	public String save_friend(){
		List<User> owners = userService.getUsers(getCurrentUerName());
		List<User> friends = userService.getUser(friend_.getUserByFriendId().getUsername());
		if (null != owners && owners.size() > 0  && null != friends && friends.size() > 0) {
			friend_.setUserByOwnerId(owners.get(0));
			friend_.setUserByFriendId(friends.get(0));
			friend_.setIsInBacklist(Constant.BOOLEAN_FALSE);
		}
		messageService.saveOrUpdateFriend(friend_);
		friend_ = null;
		return SUCCESS;
	}
	
	/**
	 * 删除好友信息
	*@author aaron.lu(luzhiyuan)
	*@return
	 */
	public String delete_friend(){
		Friend delete_friend = messageService.getFriend(new Integer(delete_friend_id));
		messageService.deleteFriend(delete_friend);
		return SUCCESS;
	}
	
	/**
	 * <P>将好友加入黑名单
	*@author aaron.lu(luzhiyuan)
	*@return
	 */
	public String update_friend(){
		Friend update_friend = messageService.getFriend(new Integer(update_friend_id));
		update_friend.setIsInBacklist(Constant.BOOLEAN_TRUE);
		messageService.saveOrUpdateFriend(update_friend);
		return SUCCESS;
	}
	
	/**
	 * 取消好友黑名单
	*@author aaron.lu(luzhiyuan)
	*@return
	 */
	public String update_friend_out_bakclist(){
		Friend update_friend = messageService.getFriend(new Integer(update_friend_id));
		update_friend.setIsInBacklist(Constant.BOOLEAN_FALSE);
		messageService.saveOrUpdateFriend(update_friend);
		return SUCCESS;
	}
	
	/**
	 * <p>获取当前用户名字
	*@author aaron.lu(luzhiyuan)
	*@return
	 */
	public String getCurrentUerName(){
		String name = "";
		Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (null != obj && obj instanceof UserDetails){
			UserDetails userDetails = (UserDetails)obj;
			name = userDetails.getUsername();
		}
		return name;
	}

	public IMessageService getMessageService() {
		return messageService;
	}

	public void setMessageService(IMessageService messageService) {
		this.messageService = messageService;
	}

	public PagerService getPagerService() {
		return pagerService;
	}

	public void setPagerService(PagerService pagerService) {
		this.pagerService = pagerService;
	}

	public List<Friend> getFriends() {
		return friends;
	}

	public void setFriends(List<Friend> friends) {
		this.friends = friends;
	}

	public Pager getFriends_pager() {
		return friends_pager;
	}

	public void setFriends_pager(Pager friends_pager) {
		this.friends_pager = friends_pager;
	}

	public String getFriends_pagerMethod() {
		return friends_pagerMethod;
	}

	public void setFriends_pagerMethod(String friends_pagerMethod) {
		this.friends_pagerMethod = friends_pagerMethod;
	}

	public String getFriends_totalRow() {
		return friends_totalRow;
	}

	public void setFriends_totalRow(String friends_totalRow) {
		this.friends_totalRow = friends_totalRow;
	}

	public String getFriends_currentPage() {
		return friends_currentPage;
	}

	public void setFriends_currentPage(String friends_currentPage) {
		this.friends_currentPage = friends_currentPage;
	}

	public List<Friend> getBacklist() {
		return backlist;
	}

	public void setBacklist(List<Friend> backlist) {
		this.backlist = backlist;
	}

	public Pager getBacklist_pager() {
		return backlist_pager;
	}

	public void setBacklist_pager(Pager backlist_pager) {
		this.backlist_pager = backlist_pager;
	}

	public String getBacklist_pagerMethod() {
		return backlist_pagerMethod;
	}

	public void setBacklist_pagerMethod(String backlist_pagerMethod) {
		this.backlist_pagerMethod = backlist_pagerMethod;
	}

	public String getBacklist_totalRow() {
		return backlist_totalRow;
	}

	public void setBacklist_totalRow(String backlist_totalRow) {
		this.backlist_totalRow = backlist_totalRow;
	}

	public String getBacklist_currentPage() {
		return backlist_currentPage;
	}

	public void setBacklist_currentPage(String backlist_currentPage) {
		this.backlist_currentPage = backlist_currentPage;
	}

	public Friend getFriend_() {
		return friend_;
	}

	public void setFriend_(Friend friend_) {
		this.friend_ = friend_;
	}

	public Friend getBack_friend() {
		return back_friend;
	}

	public void setBack_friend(Friend back_friend) {
		this.back_friend = back_friend;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public String getDelete_friend_id() {
		return delete_friend_id;
	}

	public void setDelete_friend_id(String delete_friend_id) {
		this.delete_friend_id = delete_friend_id;
	}

	public String getUpdate_friend_id() {
		return update_friend_id;
	}

	public void setUpdate_friend_id(String update_friend_id) {
		this.update_friend_id = update_friend_id;
	}
	
	
	
}
